package com.itheima.controller;


import com.github.pagehelper.PageInfo;
import com.itheima.pojo.Result;
import com.itheima.pojo.StatusCode;
import com.itheima.pojo.SysDictType;
import com.itheima.service.SysDictTypeService;
import com.itheima.pojo.SysDictTypeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/sysDictType")
public class SysDictTypeController {

    @Autowired
    private SysDictTypeService sysDictTypeService;

    //1.获取字典类型列表
    @PostMapping("/getList")
    public Map<String,Object> getList(@RequestBody SysDictTypeVO sysDictTypeVO){
        PageInfo<SysDictType>  pageInfo = sysDictTypeService.getList(sysDictTypeVO);

        Map<String,Object> result = new HashMap<>();

        Map<String,Object> recordsMap = new HashMap<>();

        recordsMap.put("records",pageInfo.getList());
        recordsMap.put("total",pageInfo.getTotal());
        recordsMap.put("size",sysDictTypeVO.getPageSize());
        recordsMap.put("current",sysDictTypeVO.getCurrentPage());
        recordsMap.put("orders","");
        recordsMap.put("optimizeCountSql",true);
        recordsMap.put("isSearchCount",true);

        result.put("code","success");
        result.put("data",recordsMap);
        return result;
    }

    //2.添加字典类型
    @PostMapping("/add")
    public Result add(@RequestBody SysDictTypeVO sysDictTypeVO){
 /*
* {
  "code": null,
  "message": "操作失败!",
  "success": 0,
  "resultcode": "00001",
  "data": null


  {
  "code": "success",
  "message": null,
  "success": null,
  "resultcode": null,
  "data": "操作成功"
}
}*/
        try{
            sysDictTypeService.add(sysDictTypeVO);
            return new Result(StatusCode.OK,"操作成功");
        }catch (Exception e){
            return new Result(StatusCode.ERROR,"操作失败");
        }
    }

    //3.删除字典类型
    @PostMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<SysDictTypeVO> sysDictTypeVOList){

        Result result=null;
        try {
            result= sysDictTypeService.deleteBatch(sysDictTypeVOList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    //4.字典类型编辑
    @PostMapping("/edit")
    public Result edit(@RequestBody SysDictTypeVO sysDictTypeVO){

        try {
            sysDictTypeService.edit(sysDictTypeVO);
            return new Result(StatusCode.OK,"更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,"更新失败");
        }
    }
}
